<?php

require_once(dirname(__FILE__) . '/../config/config.inc.php');
if ($_POST) {
    if ($_POST['page'] == 'removead') {
        $id = $_POST['id'];
        foreach (DbGet::products_pictures($id) as $picture) {
            if (file_exists(_PDT_DIR_ . 'view/' . $picture->p_filepicture))
                unlink(_PDT_DIR_ . 'view/' . $picture->p_filepicture);
            if (file_exists(_PDT_DIR_ . 'deal/' . $picture->p_filepicture))
                unlink(_PDT_DIR_ . 'deal/' . $picture->p_filepicture);
            if (file_exists(_PDT_DIR_ . 'last-updated/' . $picture->p_filepicture))
                unlink(_PDT_DIR_ . 'last-updated/' . $picture->p_filepicture);
            DbRem::products_pictures($pictures_excluded);
        }
        if(DbRem::products($id)){
            echo "OK";
        }else{
            echo "ERRO";
        }
    } else {
        $session = new Session('account');
        $data = array(
            'name' => $_POST['name'],
            'id_category' => $_POST['category'],
            'price' => Util::parse_float($_POST['price']),
            'description' => $_POST['description'],
            'dateinclusion' => date('Y-m-d')
        );
        if (!$_POST['id']) {
            if ($id = DbSet::products($data)) {
                $owner = array(
                    'id_client' => $session->id,
                    'id_product' => $id,
                    'type' => $session->type,
                );
                DbSet::products_owners($owner);
            }
        } else if (isset($_POST['id'])) {
            $id = $_POST['id'];
            if (!empty($_POST['pictures-excluded'])) {
                $pictures_excluded = explode(',', $_POST['pictures-excluded']);
                foreach (DbGet::products_pictures($_POST['id']) as $picture) {
                    echo $picture->p_id;
                    if (in_array($picture->id, $pictures_excluded)) {
                        if (file_exists(_PDT_DIR_ . 'view/' . $picture->p_filepicture))
                            unlink(_PDT_DIR_ . 'view/' . $picture->p_filepicture);
                        if (file_exists(_PDT_DIR_ . 'deal/' . $picture->p_filepicture))
                            unlink(_PDT_DIR_ . 'deal/' . $picture->p_filepicture);
                        if (file_exists(_PDT_DIR_ . 'last-updated/' . $picture->p_filepicture))
                            unlink(_PDT_DIR_ . 'last-updated/' . $picture->p_filepicture);
                    }
                }
                DbRem::products_pictures($pictures_excluded);
            }
            DbSet::products($data, $_POST['id']);
        }
        foreach ($_FILES['pictures']['tmp_name'] as $tmp_name) {
            if (empty($tmp_name))
                break;
            $filename = md5(uniqid(time())) . '.jpg';
            list($img_width, $img_height, $imagetype) = @getimagesize($tmp_name);
            switch ($imagetype) {
                case IMAGETYPE_GIF:
                    $src_img = @imagecreatefromgif($tmp_name);
                    break;
                case IMAGETYPE_JPEG:
                    $src_img = @imagecreatefromjpeg($tmp_name);
                    break;
                case IMAGETYPE_PNG:
                    $src_img = @imagecreatefrompng($tmp_name);
                    break;
            }

            $thumbs = array(
                array(
                    'width' => 650,
                    'height' => 488,
                    'dir' => _PDT_DIR_ . 'view/'
                ),
                array(
                    'width' => 310,
                    'height' => 210,
                    'dir' => _PDT_DIR_ . 'deal/'
                ),
                array(
                    'width' => 120,
                    'height' => 77,
                    'dir' => _PDT_DIR_ . 'last-updated/'
                ),
            );

            foreach ($thumbs as $thumb) {
                $width = $thumb["width"];
                $height = $thumb["height"];
                $scale = max(
                        $width / $img_width, $height / $img_height
                );
                if ($scale > 1)
                    $scale = 1;
                $new_width = $img_width * $scale;
                $new_height = $img_height * $scale;
                $new_x = ($new_width > $width) ? $new_width - $width : 0;
                $new_y = ($new_height > $height) ? $new_height - $height : 0;
                $new_img = imagecreatetruecolor($new_width - $new_x, $new_height - $new_y);
                if ($src_img) {
                    imagecopyresampled(
                            $new_img, $src_img, 0 - $new_x / 2, 0 - $new_y / 2, 0, 0, $new_width, $new_height, $img_width, $img_height
                    );
                    if (file_exists($thumb['dir'] . 'logo.png')) {
                        $logo_img = @imagecreatefrompng($thumb['dir'] . 'logo.png');
                        $logo_width = imagesx($logo_img);
                        $logo_height = imagesy($logo_img);
                        imagecopy(
                                $new_img, $logo_img, 5, $height - $logo_height - 5, 0, 0, $logo_width, $logo_height
                        );
                    }
                    imagejpeg($new_img, $thumb['dir'] . $filename);
                }
                imagedestroy($new_img);
            }
            imagedestroy($src_img);
            if (file_exists($thumbs[0]['dir'] . $filename)) {
                $data = array(
                    'id_product' => $id,
                    'filepicture' => $filename
                );
                DbSet::products_pictures($data);
            }
        }
        header('Location: ' . __BASE_URI__ . 'painel/gerenciar-anuncios/produtos');
    }
}
?>